package com.nl.deal;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.util.StringUtils;
import com.nl.DealDataToSendMain;
import com.nl.redis.RedisCluster;
import com.nl.util.ConfigUtils;
import com.nl.util.ConnectionManager;
import com.sun.tools.extcheck.Main;

import sun.tools.tree.NewArrayExpression;



public class SynchDictData implements Runnable {
	
	private static final Logger log = LoggerFactory.getLogger(SynchDictData.class);
	private StringBuilder stationMgrInfoRelationSql = new StringBuilder();
	private StringBuilder smsTemplateSql = new StringBuilder();
	private StringBuilder thresholdSql = new StringBuilder();
	private StringBuilder xjMsisdnSql = new StringBuilder();
	private StringBuilder blackUserCitySql = new StringBuilder();
	private StringBuilder blackUserProvinceSql = new StringBuilder();
	private StringBuilder blackUserCountySql = new StringBuilder();
	private StringBuilder blackUserMgrSql = new StringBuilder();
	private StringBuilder blackUserMgrSql2 = new StringBuilder();
	
	//号段
	private StringBuilder blackUserProvinceNumberDuan = new StringBuilder();
	private StringBuilder blackUserCityNumberDuan = new StringBuilder();
	private StringBuilder blackUserCountyNumberDuan = new StringBuilder();
	private StringBuilder blackUserMgrNumberDuan = new StringBuilder();
	
	//项目分组发送
	private StringBuilder mgrGroupSql = new StringBuilder();
	private StringBuilder mgrGroupRelSql = new StringBuilder();
	
	private StringBuilder msgContentsSql = new StringBuilder();
	//同步数据库中所有项目发送量
	private StringBuilder allMgrThresholdSql = new StringBuilder();
	public static int upPerLimit = Integer.valueOf(ConfigUtils.getInstance().getValue("upPerLimit"));
	public static int lowerPerLimit = Integer.valueOf(ConfigUtils.getInstance().getValue("lowerPerLimit"));
	public static int thresholdControl = Integer.valueOf(ConfigUtils.getInstance().getValue("thresholdControl"));
	private int otherSynchCount = 0;
	private String rediscfg;
	private RedisCluster cluster;
	private boolean first = true;
	public SynchDictData(String rediscfg) {
		this.rediscfg = rediscfg;
	}
	
	@Override
	public void run() {
		init();
		while(true) {
			try{
				synchThreshold();
				synchDict();
				synchOther();
			} catch (Exception e) {
				e.printStackTrace();
				log.error(e.toString());
			}
			try {
				Thread.sleep(10*1000);
				otherSynchCount ++;
				if (otherSynchCount == 30) {
					synchDict();
					synchOther();
					otherSynchCount = 0;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	
	private void init() {
		xjMsisdnSql.append("select msisdn_start,msisdn_end from xj_msisdn");
		
		blackUserCitySql.append("select PHONE_NUMBER,CITY_ID from BLACK_USERS where black_level=2 and number_type = 1");
		blackUserProvinceSql.append("select PHONE_NUMBER from BLACK_USERS where black_level=1 and number_type = 1");
		blackUserCountySql.append("select PHONE_NUMBER,county_id from BLACK_USERS where black_level=3 and number_type = 1");
		blackUserMgrSql.append("select PHONE_NUMBER,mgr_id from BLACK_USERS where black_level=4 and number_type = 1");
		//号段过滤
		blackUserMgrSql2.append("select PHONE_NUMBER,mgr_id from BLACK_USERS where black_level=5 and number_type = 1");
		
		blackUserProvinceNumberDuan.append("select PHONE_NUMBER from BLACK_USERS where black_level=1 and number_type = 2");
		blackUserCityNumberDuan.append("select PHONE_NUMBER,CITY_ID from BLACK_USERS where black_level=2 and number_type = 2");
		blackUserCountyNumberDuan.append("select PHONE_NUMBER,county_id from BLACK_USERS where black_level=3 and number_type = 2");
		blackUserMgrNumberDuan.append("select PHONE_NUMBER,mgr_id from BLACK_USERS where black_level=4 and number_type = 2");
		
		
		//项目分组发送
		mgrGroupSql.append("select group_id,SAVE_TIME from sms_mgr_group_info where STATE = 1");
		mgrGroupRelSql.append("select mgr_id from sms_mgr_group_rel where state = 1 and group_id = ?");
		
		
		
		
		thresholdSql.append("select a.id, ") 
		.append("a.is_threshold,  ") 
		.append("a.threshold,  ") 
		.append("a.cycle,  ") 
		.append("a.cycle_threshold,  ") 
		.append(" a.threshold_start_time,  ") 
		.append(" a.threshold_end_time, ") 
		.append(" b.total_count, ") 
		.append(" b.year_count, ") 
		.append(" b.quarter_count, ") 
		.append(" b.month_count,  ") 
		.append(" b.week_count,  ") 
		.append(" b.day_count, ") 
		.append(" b.time_count, ") 
		.append(" b.modify_time, ") 
		.append(" b.status," )
		.append( "to_char(b.flag_time,'yyyymmdd')  as today") 
		.append(" from sms_template_attr a,  ") 
		.append(" sms_realtime_control b, ") 
		.append(" sms_mgr_info c  ") 
		.append(" where a.id = b.id  ") 
		.append(" and a.id = c.id  ") 
		.append(" and a.state = 2  ") 
		.append(" and c.state = 0 and c.OPEN_ACCOUNT = 1 and c.IS_ARREARAGE=0 ") 
		.append(" and trunc(b.flag_time) = to_date(?,'yyyyMMdd') ");
		
		smsTemplateSql.append("select t.id, ")
		.append("				a.county_id,")
		.append("				t.mgr_type,")
		.append("               t.state, ")
		.append("               to_char(t.start_date,'yyyy-mm-dd hh24:mi:ss') start_date, ")
		.append("               t.real_time, ")
		.append("               t.is_local, ")
		.append("               t.local_is_cz, ")
		.append("               t.nonlocal_is_cz, ")
		.append("               t.local_date, ")
		.append("               t.nonlocal_date, ")
		.append("               t.IS_PROVINCE, ")
		.append("               t.SEND_TYPE, ")
		.append("               t.MODE_ID, ")
		.append("               to_char(decode(t.end_date, null, trunc(sysdate + 1), t.end_date),'yyyy-mm-dd hh24:mi:ss') end_date, ")
		.append("               to_char(decode(t.everyday_end_time, ")
		.append("                      null, ")
		.append("                      trunc(sysdate), ")
		.append("                      to_date(to_char(sysdate, 'yyyyMMdd') || ' ' || ")
		.append("                              t.everyday_start_time, ")
		.append("                              'yyyyMMdd hh24:mi:ss')),'yyyy-mm-dd hh24:mi:ss') start_time, ")
		.append("               to_char(decode(t.everyday_end_time, ")
		.append("                      null, ")
		.append("                      trunc(sysdate + 1), ")
		.append("                      to_date(to_char(sysdate, 'yyyyMMdd') || ' ' || ")
		.append("                              t.everyday_end_time, ")
		.append("                              'yyyyMMdd hh24:mi:ss')),'yyyy-mm-dd hh24:mi:ss') end_time ")
		.append("          from sms_template_attr t join sms_mgr_info a on t.id=a.id")
		.append("         where t.state = 2 and a.OPEN_ACCOUNT = 1 and a.IS_ARREARAGE=0");
		
		stationMgrInfoRelationSql.append("select ")
		.append("a.station_id, ")
		.append("b.city_ext_id, ")
		.append("a.region_id, ")
		.append("a.region_name, ")
		.append("a.station_name, ")
		.append("b.id, ")
		.append("b.name ")
		.append("from sms_base_station a, sms_mgr_info b ")
		.append("where a.id = b.id ")
		.append("and b.state = 0 and b.OPEN_ACCOUNT = 1 and b.IS_ARREARAGE=0 ");
		
		msgContentsSql.append("select ")
		.append(" decode(sign(t.effect_time-sysdate),1,t.content,-1,t.new_content,null,t.content) content,")
		.append(" t.id from sms_content t");
		
		allMgrThresholdSql.append("  select t.id,t.time_count,t.day_count,t.week_count,t.month_count,t.quarter_count,t.year_count,t.total_count from sms_realtime_control t");

	}
	
	
	private void synchDict() throws SQLException {
		//黑名单用户
		Map<String, String> blackUserTmp = new HashMap<String, String>();
		
		//项目短信内容，key项目id
		Map<String, String> msgContensTmp = new HashMap<String, String>();
		Map<String, String> xjMsisdnsTmp = new HashMap<String, String>();
		
		//项目分组列表
		Map<String, String> mgrGroupTmp = new HashMap<String,String>();
		
		
		 Connection conn = null;
	     ResultSet rs = null;
	     PreparedStatement pres = null;
	     ResultSet mgrGroupRelrs = null;
	     conn = ConnectionManager.getInstance().getConnection();
         pres = conn.prepareStatement(blackUserCitySql.toString());
         rs = pres.executeQuery();
         while(rs.next()) {
        	 try{
        		 String msisdn = rs.getString("PHONE_NUMBER");
            	 String city = rs.getString("CITY_ID");
            	 blackUserTmp.put(msisdn+"#"+city,"1");
        	 } catch (Exception e) {
        		 e.printStackTrace();
        	 }
         }
         
         pres = conn.prepareStatement(blackUserProvinceSql.toString());
         rs = pres.executeQuery();
         while(rs.next()) {
        	 try{
        		 String msisdn = rs.getString("PHONE_NUMBER");
        		 blackUserTmp.put(msisdn, "1");
        	 } catch (Exception e) {
        		 e.printStackTrace();
        	 }
         }
         
         
         pres = conn.prepareStatement(blackUserCountySql.toString());
         rs = pres.executeQuery();
         while(rs.next()) {
        	 try{
        		 String msisdn = rs.getString("PHONE_NUMBER");
        		 String countyId = rs.getString("county_id");
        		 blackUserTmp.put(msisdn+"#"+countyId, "1");
        	 } catch (Exception e) {
        		 e.printStackTrace();
        	 }
         }
         
         
         pres = conn.prepareStatement(blackUserMgrSql.toString());
         rs = pres.executeQuery();
         while(rs.next()) {
        	 try{
        		 String msisdn = rs.getString("PHONE_NUMBER");
        		 String mgrId = rs.getString("mgr_id");
        		 blackUserTmp.put(msisdn+"#"+mgrId, "1");
        	 } catch (Exception e) {
        		 e.printStackTrace();
        	 }
         }
         
         int mgrGroupCount = 0;
         int mgrGroupRelCount = 0;
         pres = conn.prepareStatement(mgrGroupSql.toString());
         rs = pres.executeQuery();
         while(rs.next()) {
        	 mgrGroupCount++;
        	 String groupId = rs.getString("GROUP_ID");
        	 String saveTime = rs.getString("SAVE_TIME");
        	 pres = conn.prepareStatement(mgrGroupRelSql.toString());
        	 pres.setString(1, groupId);
        	 mgrGroupRelrs = pres.executeQuery();
        	 while(mgrGroupRelrs.next()) {
        		 mgrGroupRelCount++;
        		 String mgrId = mgrGroupRelrs.getString("mgr_id");
        		 mgrGroupTmp.put(mgrId, groupId+"#"+saveTime);
        	 }
         }
         
         if (!mgrGroupTmp.isEmpty()) {
        	 DataCache.mgrGroupMap = mgrGroupTmp;
        	 log.info("---mgrGroup synch successful,mgrGroupCount size "+mgrGroupCount+",mgrGroupRelCount size "+mgrGroupRelCount);
         }
         
         
         
         
         //按号段过滤
         
//         pres = conn.prepareStatement(blackUserMgrSql2.toString());
//         rs = pres.executeQuery();
//         int msisdnSub = 0;
//         while(rs.next()) {
//        	 msisdnSub++;
//        	 try{
//        		 String msisdn = rs.getString("PHONE_NUMBER");
//        		 String mgrId = rs.getString("mgr_id");
//        		 blackUserTmp.put(msisdn+"#"+mgrId, "1");
//        	 } catch (Exception e) {
//        		 e.printStackTrace();
//        	 }
//         }
//         log.info("-------msisdnSub--------"+msisdnSub);
         
         
         
         int haoDuan = 0;
         pres = conn.prepareStatement(blackUserCityNumberDuan.toString());
         rs = pres.executeQuery();
         while(rs.next()) {
        	 try{
        		 haoDuan++;
        		 String msisdn = rs.getString("PHONE_NUMBER");
            	 String city = rs.getString("CITY_ID");
            	 blackUserTmp.put(msisdn+"#"+city,"1");
        	 } catch (Exception e) {
        		 e.printStackTrace();
        	 }
         }
         
         pres = conn.prepareStatement(blackUserProvinceNumberDuan.toString());
         rs = pres.executeQuery();
         while(rs.next()) {
        	 try{
        		 haoDuan++;
        		 String msisdn = rs.getString("PHONE_NUMBER");
        		 blackUserTmp.put(msisdn, "1");
        	 } catch (Exception e) {
        		 e.printStackTrace();
        	 }
         }
         
         
         pres = conn.prepareStatement(blackUserCountyNumberDuan.toString());
         rs = pres.executeQuery();
         while(rs.next()) {
        	 try{
        		 haoDuan++;
        		 String msisdn = rs.getString("PHONE_NUMBER");
        		 String countyId = rs.getString("county_id");
        		 blackUserTmp.put(msisdn+"#"+countyId, "1");
        	 } catch (Exception e) {
        		 e.printStackTrace();
        	 }
         }
         
         
         pres = conn.prepareStatement(blackUserMgrNumberDuan.toString());
         rs = pres.executeQuery();
         while(rs.next()) {
        	 try{
        		 haoDuan++;
        		 String msisdn = rs.getString("PHONE_NUMBER");
        		 String mgrId = rs.getString("mgr_id");
        		 blackUserTmp.put(msisdn+"#"+mgrId, "1");
        	 } catch (Exception e) {
        		 e.printStackTrace();
        	 }
         }
         
         log.info("haoDuan blackUser synch successful, size = "+haoDuan);
         
         if (!blackUserTmp.isEmpty()) {
        	 DataCache.blackUser = blackUserTmp;
        	 log.info("blackUser synch successful,blackUser size "+DataCache.blackUser.size());
         }
         
         
         
         pres = conn.prepareStatement(msgContentsSql.toString());
         rs = pres.executeQuery();
         while(rs.next()) {
        	String id = rs.getString("id");
        	try{
        		String content = rs.getString("content");
        		msgContensTmp.put(id, content);
        	} catch (Exception e) {
        		log.error("project "+id+" msgContens synch failed!",e);
        	}
         }
         
         
         
         
         
         
         if (!msgContensTmp.isEmpty()) {
        	 DataCache.msgContens = msgContensTmp;
        	 log.info("msgContens synch successful,msgContens size "+DataCache.msgContens.size());
         }
         
         pres = conn.prepareStatement(xjMsisdnSql.toString());
         rs = pres.executeQuery();
         while(rs.next()) {
        	String msisdnStart = rs.getString("msisdn_start");
        	try{
        		String msisdnEnd = rs.getString("msisdn_end");
        		xjMsisdnsTmp.put(msisdnStart, msisdnEnd);
        	} catch (Exception e) {
        		log.error("xjMsisdn "+msisdnStart+" MsisdnStart synch failed!",e);
        	}
         }
         if (! xjMsisdnsTmp.isEmpty()) {
			DataCache.xjMsisdns = xjMsisdnsTmp;
			log.info("xjMsisdn synch successful,xjMsisdn size"+DataCache.xjMsisdns.size());
		}
         if (null != rs) {
        	 rs.close();
         }
         
         if (null != pres) {
        	 pres.close();
         }
         
         if (null != conn) {
        	 conn.close();
         }
		DealDataToSendMain.dictInitSuccessful = true;
		log.info("all dict data synch successful! status = "+DealDataToSendMain.dictInitSuccessful);
	}
	
	
	private void synchThreshold() throws SQLException, IOException {
		log.info("start threshold");
		//只在第一次同步时加载已发送量
		if (first) {
			log.info("first");
			cluster = new RedisCluster(rediscfg);
		}
//		log.info("first .....");
		//阀值控制表
		Map<String, ThresholdInfo> thresholdMapTmp = new HashMap<String, ThresholdInfo>();
		//项目id存入redis中
		Map<String, String> mgrs=new HashMap<String,String>();
		int count=0;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Connection conn = null;
		ResultSet rs = null;
	    PreparedStatement pres = null;  	     
	    conn = ConnectionManager.getInstance().getConnection();

	    //同步数据库中的所有发送量数据
	    if (null != cluster) {
	    pres = conn.prepareStatement(allMgrThresholdSql.toString());
	    rs = pres.executeQuery();
	    while(rs.next()) {
	    	try {
	    		String mgrId = rs.getString("id");
		       	String timeCount=rs.getString("time_count");
		       	String dayCount=rs.getString("day_count");
		       	String weekCount=rs.getString("week_count");
		       	String monthCount=rs.getString("month_count");
		       	String quarterCount=rs.getString("quarter_count");
		       	String yearCount=rs.getString("year_count");
		       	String totalCount=rs.getString("total_count");
		       	
		       		if(!mgrs.containsKey(mgrId)){
                		mgrs.put(mgrId, "1");
                		cluster.hmset("mgrs", mgrs);
                		count++;
                	}
		       		int dbTotalCount=Integer.valueOf(totalCount);
		       		int redisTotalCount=Integer.valueOf(cluster.get(mgrId+"totalCount"));
		       		cluster.set(mgrId+"dayCount",dayCount);
		       		if(dbTotalCount>redisTotalCount){
		       			cluster.set(mgrId+"timeCount",timeCount);
		            	cluster.set(mgrId+"weekCount", weekCount);
		            	cluster.set(mgrId+"monthCount",monthCount);
		            	cluster.set(mgrId+"quarterCount",quarterCount);
		            	cluster.set(mgrId+"yearCount",yearCount);
		            	cluster.set(mgrId+"totalCount",totalCount);
		            	log.info(mgrId+":timeCount:"+timeCount+";dayCount"+dayCount+";weekCount:"+weekCount+";monthCount:"+monthCount+";quarterCount:"+quarterCount+";yearCount:"+yearCount+";totalCount"+totalCount);
		       		}
	    	} catch (Exception e) {
	    		log.error("redis同步数据中的数据时发生异常:"+e.toString());
	    		e.printStackTrace();
	    	}
	    }
	    log.info("first sync success ,total:"+count);
	    }
	     
        pres = conn.prepareStatement(thresholdSql.toString());
//        log.info(thresholdSql.toString());
        pres.setString(1, sdf.format(new Date()));
//        log.info("now time "+sdf.format(new Date()));
        rs = pres.executeQuery();
        while(rs.next()) {
       	 String id = rs.getString("ID");
       	 try{
       		 ThresholdInfo tInfo = new ThresholdInfo();
                tInfo.setMgrId(id);
                tInfo.setIsThreshold(rs.getInt("IS_THRESHOLD"));
                long totalThreshold = rs.getLong("THRESHOLD");
                long cycleThreshold = rs.getLong("CYCLE_THRESHOLD");
                
                if (totalThreshold > thresholdControl) {
                	totalThreshold = totalThreshold * upPerLimit / 100;
                } else {
                	totalThreshold = totalThreshold * lowerPerLimit / 100;
                }
                
                if (cycleThreshold > thresholdControl) {
                	cycleThreshold = cycleThreshold * upPerLimit / 100;
                } else {
                	cycleThreshold = cycleThreshold * lowerPerLimit / 100;
                }
                tInfo.setTotalThreshold(totalThreshold);
                tInfo.setCycle(rs.getInt("CYCLE"));
                tInfo.setCycleThreshold(cycleThreshold);
                tInfo.setThresholdStartTime(rs.getTimestamp("THRESHOLD_START_TIME"));
                tInfo.setThresholdEndTime(rs.getTimestamp("THRESHOLD_END_TIME"));
                long dayCount = 0;
                if (rs.getInt("STATUS") == 1) {
                    tInfo.setTotalCount(0);
                    tInfo.setYearCount(0);
                    tInfo.setQuarterCount(0);
                    tInfo.setMonthCount(0);
                    tInfo.setWeekCount(0);
                    tInfo.setDayCount(0);
                    tInfo.setTimeCount(0);
                    tInfo.setStatus(2);//已重置
                } else {
                	dayCount = rs.getLong("DAY_COUNT");
                    tInfo.setTotalCount(rs.getLong("TOTAL_COUNT"));
                    tInfo.setYearCount(rs.getLong("YEAR_COUNT"));
                    tInfo.setQuarterCount(rs.getLong("QUARTER_COUNT"));
                    tInfo.setMonthCount(rs.getLong("MONTH_COUNT"));
                    tInfo.setWeekCount(rs.getLong("WEEK_COUNT"));
                    tInfo.setDayCount(dayCount);
                    tInfo.setTimeCount(rs.getLong("TIME_COUNT"));
                    tInfo.setStatus(rs.getInt("STATUS"));
                }
                tInfo.setModyfyTime(rs.getString("MODIFY_TIME"));
                tInfo.setToday(rs.getString("TODAY"));
                thresholdMapTmp.put(id, tInfo);

       	 } catch (Exception e) {
       		 log.error("project "+id+" threshold synch failed!",e);
       	 }
        }
        if(first){
        	log.info("第一次同步数据库中数据到redis中，项目数量为"+count);
        }
        
        
       	 
        if (null != rs) {
       	 rs.close();
        }
        
        if (null != pres) {
       	 pres.close();
        }
        
        if (null != conn) {
       	 conn.close();
        }
        
        if (null != cluster) {
        	cluster.close();
        	cluster = null;
        }
        if (!thresholdMapTmp.isEmpty()) {
        	DealDataToSendMain.thresholdUpdateSuccess = true;
		   	 DataCache.thresholdMap = thresholdMapTmp;
		   	 log.info("Threshold synch successful,threshold size "+DataCache.thresholdMap.size());
        }
        first = false;
	}
	
	private void synchOther() throws SQLException {
			
			//短信模板表,key项目id
			Map<String, SmsTemplateAttr> smsTemplateAttrMapTmp = new HashMap<String, SmsTemplateAttr>();
			
			//短信信息和基站关联表，key项目id
			Map<String, StationMgrRelation> stationMgrINfoRelationMapTmp = new HashMap<String, StationMgrRelation>();
			
			//基站和项目对应关系，key基站id
			Map<String, String> stationMgrRelationMapTmp = new HashMap<String, String>();
					
			Connection conn = null;
		   ResultSet rs = null;
		     PreparedStatement pres = null;
		     conn = ConnectionManager.getInstance().getConnection();
	        pres = conn.prepareStatement(smsTemplateSql.toString());
	        rs = pres.executeQuery();
	        while(rs.next()) {
	       	String id = rs.getString("id");
	       	try{
	       		String state = rs.getString("state");
	       		String countyId = rs.getString("county_id");
	       		String mgrType = rs.getString("mgr_type");
	       		String start_date = rs.getString("start_date");
	           	String real_time = rs.getString("real_time");
	           	String is_local = rs.getString("is_local");
	           	String local_is_cz = rs.getString("local_is_cz");
	           	String nonlocal_is_cz = rs.getString("nonlocal_is_cz");
	           	String end_date = rs.getString("end_date");
	           	String start_time = rs.getString("start_time");
	           	String end_time = rs.getString("end_time");
	           	String is_pronivce=rs.getString("IS_PROVINCE");
	           	String local_date = rs.getString("local_date");
	           	String nonlocal_date = rs.getString("nonlocal_date");
	           	String sendType = rs.getString("send_type");
	           	String modeId = rs.getString("mode_id");
	           	SmsTemplateAttr smsTemplateAttr = new SmsTemplateAttr();
	           	smsTemplateAttr.setMgrType(mgrType);
	           	smsTemplateAttr.setCountyId(countyId);
	           	smsTemplateAttr.setId(id);
	           	smsTemplateAttr.setIs_pronivce(is_pronivce);
	           	smsTemplateAttr.setState(state);
	           	smsTemplateAttr.setStart_date(start_date);
	           	smsTemplateAttr.setReal_time(real_time);
	           	smsTemplateAttr.setIs_local(is_local);
	           	smsTemplateAttr.setLocal_is_cz(local_is_cz);
	           	smsTemplateAttr.setNonlocal_is_cz(nonlocal_is_cz);	           	
	           	//smsTemplateAttr.setIs_Cz(is_Cz);
	           	smsTemplateAttr.setEnd_date(end_date);
	           	smsTemplateAttr.setStart_time(start_time);
	           	smsTemplateAttr.setEnd_time(end_time);
	           	smsTemplateAttr.setLocal_date(local_date);
	           	smsTemplateAttr.setNonlocal_date(nonlocal_date);
	           	smsTemplateAttr.setSendType(sendType);
	           	smsTemplateAttr.setModeId(modeId);
	           	smsTemplateAttrMapTmp.put(id, smsTemplateAttr);
	       	} catch (Exception e) {
	       		log.error("project "+id+" smsTemplateAttr synch failed!",e);
	       	}
	        }
	        
	        if (!smsTemplateAttrMapTmp.isEmpty()) {
	       	 DataCache.smsTemplateAttrMap = smsTemplateAttrMapTmp;
	       	 log.info("smsTemplateAttr synch successful,smsTemplateAttr size "+DataCache.smsTemplateAttrMap.size());
	        }
	        
	        
	        pres = conn.prepareStatement(stationMgrInfoRelationSql.toString());
	        rs = pres.executeQuery();
	        while(rs.next()) {
	       	 String station_id = rs.getString("station_id");
	       	 try {
	       		 String city_id = rs.getString("city_ext_id");
	           	 String region_id = rs.getString("region_id");
	           	 String region_name = rs.getString("region_name");
	           	String station_name = rs.getString("station_name");
	           	String id = rs.getString("id");
	           	//String check_path = rs.getString("check_path");
	           	String name = rs.getString("name");
	           	StationMgrRelation stationMgrRelation = new StationMgrRelation();
	           	stationMgrRelation.setCity_id(city_id);
	           	//stationMgrRelation.setCheck_path(check_path);
	           	stationMgrRelation.setId(id);
	           	stationMgrRelation.setName(name);
	           	stationMgrRelation.setRegion_id(region_id);
	           	stationMgrRelation.setRegion_name(region_name);
	           	stationMgrRelation.setStation_name(station_name);
	           	stationMgrINfoRelationMapTmp.put(id+"#"+station_id, stationMgrRelation);
	           	if (stationMgrRelationMapTmp.containsKey(station_id)) {
	           		stationMgrRelationMapTmp.put(station_id, stationMgrRelationMapTmp.get(station_id)+","+id);
	           	} else {
	           		stationMgrRelationMapTmp.put(station_id, id);
	           	}
	           	
	       	 } catch (Exception e) {
	       		 log.error("station "+station_id+" station info synch failed!",e);
	       	 }
	        }
	        
	        
	        if (!stationMgrINfoRelationMapTmp.isEmpty()) {
	       	 DataCache.stationMgrINfoRelationMap = stationMgrINfoRelationMapTmp;
	       	 log.info("stationMgrINfoRelation synch successful,stationMgrINfoRelation size "+DataCache.stationMgrINfoRelationMap.size());
	        }
	        
	        if (!stationMgrRelationMapTmp.isEmpty()) {
	       	 DataCache.stationMgrRelationMap = stationMgrRelationMapTmp;
	       	 log.info("stationMgrRelation synch successful,stationMgrRelation size "+DataCache.stationMgrRelationMap.size());
	        } 
	       	 
	        if (null != rs) {
	       	 rs.close();
	       	 
	        }
	        
	        if (null != pres) {
	       	 pres.close();
	        }
	        
	        if (null != conn) {
	       	 conn.close();
	        }
	}
}
